package patrol.mobile.com.mobilepatrol.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.bean.RouteSurveyTaskStructBO;
import patrol.mobile.com.mobilepatrol.bean.TrafficInvestBaseInfo;

/**
 * Created by 健 on 2014/8/27.
 */
public class InvestDetailCatalogAdapter extends BaseExpandableListAdapter {

    private int selectedItemIndex=-1;
    private int selectedChildIndex = -1;
    private Context context;
    private DBHelper dbHelper;
    private TrafficInvestBaseInfo investBaseInfo;

    private List<RouteSurveyTaskStructBO> group=new ArrayList<RouteSurveyTaskStructBO>();
    private Map<String,List<RouteSurveyTaskStructBO>> groupItems=new HashMap<String, List<RouteSurveyTaskStructBO>>();

    private List<String> structIds;

    public InvestDetailCatalogAdapter(Context context, DBHelper dbHelper, TrafficInvestBaseInfo investBaseInfo) {
        this.context=context;
        this.dbHelper=dbHelper;
        this.investBaseInfo = investBaseInfo;
        initData();
    }

    private void initData() {
        String state=investBaseInfo.getSurveyType().equals("复核")?"复核":"";
        group=dbHelper.getRouteSurveyTaskStructInfo(investBaseInfo.getDetailID(),state,investBaseInfo);
        for (int i=0;i<group.size();i++){
            RouteSurveyTaskStructBO data = group.get(i);
            int start_count = 0;
            int child_count;
            double roadLength = data.roadLength;
            int startLength = data.getPotMeter();
            if (roadLength%100==0){
                child_count = (int) (roadLength/100);
            }else{
                child_count = (int) (roadLength/100)+1;
            }
            if (startLength>=0&&startLength<100){
                start_count = 0;
            }
            if (startLength>=100&&startLength<200){
                start_count = 1;
            }
            if (startLength>=200&&startLength<300){
                start_count = 2;
            }
            if (startLength>=300&&startLength<400){
                start_count = 3;
            }
            if (startLength>=400&&startLength<500){
                start_count = 4;
            }
            if (startLength>=500&&startLength<600){
                start_count = 5;
            }
            if (startLength>=600&&startLength<700){
                start_count = 6;
            }
            if (startLength>=700&&startLength<800){
                start_count = 7;
            }
            if (startLength>=800&&startLength<900){
                start_count = 8;
            }
            if (startLength>=900&&startLength<1000){
                start_count = 9;
            }
            for (int j=0;j<child_count;j++){
                data.counts.add((start_count+j));
            }

        }
        structIds=new ArrayList<String>();
        for(RouteSurveyTaskStructBO struct:group){
            structIds.add(struct.getiD());
        }
        selectedItemIndex=dbHelper.getLastUpdateStructID(structIds);
        if (selectedItemIndex!=-1)
        selectedChildIndex = dbHelper.getLastchildID(structIds.get(selectedItemIndex));
    }

    public void updateSelectedItem(){
        selectedItemIndex=dbHelper.getLastUpdateStructID(structIds);
        if (selectedItemIndex!=-1)
            selectedChildIndex = dbHelper.getLastchildID(structIds.get(selectedItemIndex));
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return group.get(i).counts.size();
    }

    @Override
    public Object getGroup(int i) {
        return group.get(i);
    }

    @Override
    public Object getChild(int i, int i2) {
        return groupItems.get(group.get(i)).get(i2);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i2) {
        return i2;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) {
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.catalog_title_view,null);
        }
        TextView tv= (TextView) view;

        RouteSurveyTaskStructBO struct= group.get(i);
        tv.setText(struct.getPot()+struct.getPotKm());

        if(i==selectedItemIndex)
            tv.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
        else
            tv.setBackgroundColor(context.getResources().getColor(R.color.transparent));


        return view;
    }

    @Override
    public View getChildView(int i, int i2, boolean isLastChild, View view, ViewGroup viewGroup) {

        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.catalog_title_view_child,null);
        }
        TextView tv= (TextView) view;

        int index = group.get(i).counts.get(i2);
        if (selectedItemIndex==i&&index==selectedChildIndex){
            tv.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
        }else{
            tv.setBackgroundColor(context.getResources().getColor(R.color.transparent));
        }
        switch (index){
            case 0:
                tv.setText("0-100");
                break;
            case 1:
                tv.setText("100-200");
                break;
            case 2:
                tv.setText("200-300");
                break;
            case 3:
                tv.setText("300-400");
                break;
            case 4:
                tv.setText("400-500");
                break;
            case 5:
                tv.setText("500-600");
                break;
            case 6:
                tv.setText("600-700");
                break;
            case 7:
                tv.setText("700-800");
                break;
            case 8:
                tv.setText("800-900");
                break;
            case 9:
                tv.setText("900-1000");
                break;
        }

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        return true;
    }
}
